package com.longcai.piaojia.adapter;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.longcai.piaojia.R;
import com.longcai.piaojia.conn.OrderCouponListGet;
import com.zcx.helper.bound.BoundView;
import com.zcx.helper.bound.BoundViewHelper;
import com.zcx.helper.scale.ScaleScreenHelperFactory;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2016/8/4.
 */
public class OrderCouponListAdapter extends ArrayAdapter<OrderCouponListGet.OrderCouponEntity> {

    private Context context;
    private LayoutInflater layoutInflater;

    public Map<Integer, Boolean> isChoose = new HashMap<>();

    List<OrderCouponListGet.OrderCouponEntity> lists = new ArrayList<>();

    public OrderCouponListAdapter(Context context, List<OrderCouponListGet.OrderCouponEntity> objects) {
        super(context, 0, objects);

        this.context = context;

        layoutInflater = ((Activity) context).getLayoutInflater();

        this.lists = objects;
    }

    public void initChoose(){

        for (int i = 0; i < lists.size(); i++) {

            isChoose.put(i, false);

        }
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        ViewHolder viewHolder = null;

        OrderCouponListGet.OrderCouponEntity couponEntity = getItem(position);

        if (convertView == null) {

            convertView = BoundViewHelper.boundView(viewHolder = new ViewHolder(), ScaleScreenHelperFactory.getInstance().loadView((ViewGroup) layoutInflater.inflate(R.layout.item_pick_order_coupon_list, null)));

            convertView.setTag(viewHolder);

        } else {

            viewHolder = (ViewHolder) convertView.getTag();

        }

        if(position==0){

            viewHolder.item_order_coupon_list_cancel_rl.setVisibility(View.VISIBLE);
            viewHolder.item_order_coupon_list_background_rl.setVisibility(View.GONE);

        }else{

            viewHolder.item_order_coupon_list_cancel_rl.setVisibility(View.GONE);
            viewHolder.item_order_coupon_list_background_rl.setVisibility(View.VISIBLE);

        }

        if (isChoose.get(position)) {

            viewHolder.item_pick_ticket_bg_iv.setSelected(true);


        } else {

            viewHolder.item_pick_ticket_bg_iv.setSelected(false);


        }

        viewHolder.item_order_coupon_list_money_tv.setText(couponEntity.money);
        viewHolder.item_order_coupon_list_type_tv.setText(couponEntity.title);
        viewHolder.item_order_coupon_list_condition_tv.setText(context.getString(R.string.coupon_list_condition_front_text) + couponEntity.total + context.getString(R.string.coupon_list_condition_back_text));
        viewHolder.item_order_coupon_list_time_limit_tv.setText(context.getString(R.string.coupon_list_condition_use_date_text) + couponEntity.starttime + context.getString(R.string.space)+ context.getString(R.string.goods_info_symbols_text) + context.getString(R.string.space) + couponEntity.endtime);

        return convertView;

    }

    private class ViewHolder {

        @BoundView(R.id.item_order_coupon_list_cancel_rl) private RelativeLayout item_order_coupon_list_cancel_rl;
        @BoundView(R.id.item_order_coupon_list_background_rl) private RelativeLayout item_order_coupon_list_background_rl;

        @BoundView(R.id.item_order_coupon_list_money_tv) private TextView item_order_coupon_list_money_tv;
        @BoundView(R.id.item_order_coupon_list_condition_tv) private TextView item_order_coupon_list_condition_tv;
        @BoundView(R.id.item_order_coupon_list_type_tv) private TextView item_order_coupon_list_type_tv;
        @BoundView(R.id.item_order_coupon_list_time_limit_tv) private TextView item_order_coupon_list_time_limit_tv;

        @BoundView(R.id.item_pick_ticket_bg_iv) private ImageView item_pick_ticket_bg_iv;

    }

}
